package cn.edu.seu.cose.qol.ws.resource.admin;

import java.util.Map;

import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;

import cn.edu.seu.cose.qol.model.dao.database.account.AdminAccountDataAccess;
import cn.edu.seu.cose.qol.model.dto.admin.AdminInstance;
import cn.edu.seu.cose.qol.model.po.account.AdminAccount;
import cn.edu.seu.cose.qol.ws.app.RequestAttributes;
import cn.edu.seu.cose.qol.ws.resource.QolServerResource;
import cn.edu.seu.cose.qol.ws.resource.util.DomRepresentationBuilder;
import cn.edu.seu.cose.qol.ws.resource.util.XmlObjectReader;
import cn.edu.seu.cose.qol.ws.resource.util.adapter.AdminAdapter;

public class AdminInstanceResource extends QolServerResource {
	
	/**
	 * @return URI所指代的管理员表征
	 */
	@Get("xml")
	public Representation getAdministrator() {
		String adminName = getAdminName();
		AdminAccountDataAccess adminAccountAccess =
				AdminAccountDataAccess.getInstance();
		try {
			AdminAccount adminAccount =
					adminAccountAccess.getAdminAccountByUsername(adminName);
			if (adminAccount == null) {
				throw new NoSuchAdmin(adminName);
			}
			String username = adminAccount.getUsername();
			String password = adminAccount.getPassword();
			
			AdminInstance transferAdmin = new AdminInstance();
			transferAdmin.setUsername(username);
			transferAdmin.setPassword(password);
			
			DomRepresentationBuilder builder  =
					DomRepresentationBuilder.getInstance();
			return builder.getRepresentation(transferAdmin);
		} catch (Exception e) {
			return getRepresentationByException(e);
		}
	}
	
	/**
	 * 更新URI所指代的管理员。若管理员已存在，则更新；若不存在，则创建。
	 * @param representation
	 * @return 创建/更新结果的表征
	 */
	@Put("xml:xml")
	public Representation updateAdministrator(Representation representation) {
		try {
			String adminName = getAdminName();
			DomRepresentation inputRepresentation = new DomRepresentation(
					representation);
			XmlObjectReader reader = XmlObjectReader.getInstance();
			AdminInstance adminInstance = (AdminInstance) reader.getXmlObject(
					inputRepresentation, AdminInstance.class);
			
			AdminAdapter adapter = AdminAdapter.getInstance();
			AdminAccount currentAdminAccount = adapter.adapt(adminInstance);
			
			AdminAccountDataAccess dao = AdminAccountDataAccess.getInstance();
			AdminAccount previousAdminAccount =
					dao.getAdminAccountByUsername(adminName);
			
			// 创建新的管理员帐户
			if (previousAdminAccount == null) {
				currentAdminAccount.setUsername(adminName);
				dao.createNewAdminAccount(currentAdminAccount);
				return getSuccessRepresentation();
			}
			
			// 更新管理员帐户
			long adminId = previousAdminAccount.getAdminAccountId();
			currentAdminAccount.setAdminAccountId(adminId);
			dao.updateAdminAccount(currentAdminAccount);
			return getSuccessRepresentation();
		} catch (Exception e) {
			return getRepresentationByException(e);
		}
	}
	
	/**
	 * 删除URI所指代的管理员。
	 * @param representation
	 * @return 删除结果的表征
	 */
	@Delete("xml:xml")
	public Representation removeAdministrator(
			Representation representation) {
		try {
			String adminName = getAdminName();
			AdminAccountDataAccess dao = AdminAccountDataAccess.getInstance();
			dao.deleteAdminAccountByUsername(adminName);
			return getSuccessRepresentation();
		} catch (Exception e) {
			return getRepresentationByException(e);
		}
	}
	
	private String getAdminName() {
		Map<String, Object> requestAttributes = getRequestAttributes();
		return (String) requestAttributes.get(
				RequestAttributes.ADMIN_NAME);
	}
	
	private static class NoSuchAdmin extends Exception {

		private static final long serialVersionUID = -3809799085039568405L;

		public NoSuchAdmin(String adminName) {
			super("no such admin: " + adminName);
		}
		
	}

}
